package com.rerun.task;

import com.rerun.common.util.CommonUtil;
import com.rerun.entity.JobFeedback;
import com.rerun.entity.JobFeedbackDetail;
import com.rerun.enums.ResumeStateEnum;
import com.rerun.service.IJobFeedbackDetailService;
import com.rerun.service.IJobFeedbackService;
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * The type Feedback detail task.
 *
 * @author dax.
 * @version v1.0
 * @since 2017 /10/12 11:14
 */
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
public class FeedbackDetailTask implements Job{
    private static final Logger log = LoggerFactory.getLogger(FeedbackDetailTask.class);
    @Resource
    private IJobFeedbackDetailService iJobFeedbackDetailService;
    @Resource
    private IJobFeedbackService iJobFeedbackService;


    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        List<JobFeedback> jobFeedbackList = iJobFeedbackService.selectListNotInDetail();
        if (!jobFeedbackList.isEmpty()) {
            List<JobFeedbackDetail> details = new ArrayList<>();
            for (JobFeedback jobFeedback : jobFeedbackList) {
                String jobFeedId = jobFeedback.getId();
                Date createTime = ResumeStateEnum.post.getKey().equals("100") ? jobFeedback.getCreateTime() : jobFeedback.getUpdateTime();
                long status = jobFeedback.getStatus();
                String remark = jobFeedback.getRemark();

                JobFeedbackDetail detail = new JobFeedbackDetail();
                detail.setJobFeedId(jobFeedId);
                detail.setCreateTime(createTime);
                detail.setStatus(status);
                detail.setRemark(remark);
                details.add(detail);
            }
            if (!details.isEmpty()){
                iJobFeedbackDetailService.insertBatch(details);
                log.info("exec batch insert new feedbackDetails");
            }
        } else {
            log.debug("{}  quartz: >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> no jobFeedback data to execute", CommonUtil.formatDate(new Date(),"yyyy-MM-dd HH:mm:ss"));
        }
    }
}
